package com.cirnoworks.utils.file;

import java.io.Closeable;
import java.io.IOException;
import java.util.IdentityHashMap;

public abstract class ResourceHandler {

	private final IdentityHashMap<Closeable, Object> resources = new IdentityHashMap<Closeable, Object>();

	protected <T extends Closeable> T addResource(T resource) {
		resources.put(resource, this);
		return resource;
	}

	protected void closeAllResources() {
		for (Closeable resource : resources.keySet()) {
			try {
				resource.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	protected abstract void run() throws IOException;
}
